// ==UserScript==
// @name         hyttest
// @namespace    http://tampermonkey.net/
// @version      2024-10-24
// @description  try to take over the world!
// @author       You
// @match        *://*.douyin.com/video/*
// @match        *://*.xiaohongshu.com/*
// @match        *://*.kuaishou.com/short-video/*
// @match        *://*.bilibili.com/video/*
// @require      https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js

// @license      MIT
// ==/UserScript==

$(document).ready(function () {
  // 加载 XLSX 库
  $.getScript(
    "https://cdn.bootcdn.net/ajax/libs/xlsx/0.18.5/xlsx.mini.min.js",
    function () {
      const downnoteButton = $("<div>")
        .attr("id", "note-download")
        .css({
          width: "120px",
          height: "40px",
          background: "#fe2c55",
          position: "fixed",
          top: "30px",
          left: "30px",
          "z-index": "100000",
          "text-align": "center",
          "line-height": "40px",
          color: "#fff",
          "border-radius": "8px",
          cursor: "pointer",
        })
        .text("主页笔记采集");
      $("body").append(downnoteButton);

      const success = $("<div>")
        .attr("id", "success-download")
        .css({
          width: "400px",
          height: "400px",
          background: "yellow",
          position: "fixed",
          top: "200px",
          "z-index": "100000",
           display:"none",
            "align-items":"center",
            "justify-content":"center",
          "text-align": "center",
          color: "red",
          "font-size": "24px",
          cursor: "pointer",
        })
        .text("主页爬取完成");
      $("body").append(success);

        // 添加滚动按钮
      const scrolnoteButton = $("<div>")
        .attr("id", "note-scroll")
        .css({
          width: "120px",
          height: "40px",
          background: "#fe2c55",
          position: "fixed",
          top: "80px",
          left: "30px",
          "z-index": "100000",
          "text-align": "center",
          "line-height": "40px",
          color: "#fff",
          "border-radius": "8px",
          cursor: "pointer",
        })
        .text("查询笔记采集");
      $("body").append(scrolnoteButton);

        //查询关键词笔记采集
        scrolnoteButton.click(function () {
          console.log("开始滚动笔记区", new Date());
          let intervalId1;
          let scrollTop1 = 0;
          const randomDelayRange = [100, 1000];
          let lastDataIndexChecked = null;
          const note_result = [];

          function checkForEndContainer() {
            const endContainer = $(".end-container");
            if (endContainer.length > 0) {
              clearInterval(intervalId1);
              console.log("已滚动到笔记区底部", new Date());
              console.log(note_result);
              var tit = $("title").text()
              var noteexcelname = (tit.replace(/\s+/g, '')) +"笔记" + $(".filter span").text() +"采集数据.xlsx";
              downtoexcel(note_result, noteexcelname);
              $("#success-download").css("display","flex")
              $(window).off("scroll", checkForNewNoteItem);
            }
          }

          function checkForNewNoteItem() {
            const newNoteItems = $(".note-item");
            newNoteItems.each(function () {
              const $item = $(this);
              const dataIndex = $item.attr("data-index");
              const href = $item.find('a[class="cover ld mask"]');
              const name = $item.find(".name");
              const titleSpan = $item.find(".title span");
              const likenum = $item.find(".count");
              if (!$item.attr("checked") &&dataIndex !== lastDataIndexChecked) {
                $item.attr("checked", "true");
                const name = $("section[data-index='" + dataIndex + "']").find(".name").text();
                const href = $("section[data-index='" + dataIndex + "']").find("a:nth-child(2)").attr("href");
                const title = $("section[data-index='" + dataIndex + "']").find(".title span").text();
                const like = $("section[data-index='" + dataIndex + "']").find(".count").text();
                like === "赞" ? "0" : like;
                console.log("新的 note-item 元素出现，data-index 值为:",dataIndex,name,href,title,like);
                note_result.push({
                  dataindex: dataIndex,
                  href: "https://www.xiaohongshu.com" + href,
                  name: name,
                  title: title,
                  like: like,
                });
                lastDataIndexChecked = dataIndex;
              }
            });
          }

          function startScrollAndCheck() {
            const randomDelay =
              Math.floor(Math.random() * (1000 - 100 + 1)) + 200;
            intervalId1 = setInterval(() => {
              scrollTop1 += 100;
              console.log("开始滚动");
              window.scrollBy(0, scrollTop1);
              console.log(scrollTop1);
              checkForNewNoteItem();
              checkForEndContainer();
            }, randomDelay);
          }
          window.addEventListener("scroll", checkForNewNoteItem);
          startScrollAndCheck();
        });


      // 个人页文章采集点击事件
      downnoteButton.click(function () {
        console.log("开始滚动笔记区", new Date());
        let intervalId1;
        let scrollTop1 = 0;
        const randomDelayRange = [100, 1000];
        let lastDataIndexChecked = null;
        const note_result = [];
        function checkForNewNoteItem() {
          const newNoteItems = $(".note-item");
          newNoteItems.each(function () {
            const $item = $(this);
            const dataIndex = $item.attr("data-index");
            const name = $item.find(".name");
            const titleSpan = $item.find(".title span");
            const likenum = $item.find(".count");
            if (!$item.attr("checked") && dataIndex !== lastDataIndexChecked) {
              $item.attr("checked", "true");
              const name = $("section[data-index='" + dataIndex + "']").find(".name").text();
              const href = $("section[data-index='" + dataIndex + "']").find("a:nth-child(2)").attr("href");
              const title = $("section[data-index='" + dataIndex + "']").find(".title span").text();
              const like = $("section[data-index='" + dataIndex + "']").find(".count").text();
              const play_icon = $item.find('.play-icon');
              like === '赞'? '0' :like;
              var note_type = '';
              var likes = '';
              if(like == '赞') { likes = 0 }else { likes = like }
              if(play_icon.length > 0){ note_type = '视频' }else{ note_type = '笔记' }
              console.log(
                "新的 note-item 元素出现，data-index 值为:",
                dataIndex,
                name,
                href,
                title,
                like
              );
              note_result.push({
                href: "https://www.xiaohongshu.com" + href,
                name: name,
                title: title,
                like: likes,
                note_type,
                info1:($('title').text()).split(' - ')[0]
              });
              lastDataIndexChecked = dataIndex;
            }
          });
        }

        function startScrollAndCheck() {
          const randomDelay =Math.floor(Math.random() * (1000 - 100 + 1)) + 200;
          let lastScrollPosition = window.scrollY; // 记录上一次滚动的位置
          intervalId1 = setInterval(() => {
            const scrollAmount = 100; // 本次滚动的距离
            console.log("开始滚动");
            window.scrollBy(0, scrollAmount);

            // 获取当前滚动位置并计算与上一次的差异
            const currentScrollPosition = window.scrollY;
            const actualScrollAmount =
              currentScrollPosition - lastScrollPosition;
            if (actualScrollAmount === 0) {
              clearInterval(intervalId1); // 停止滚动
              console.log("滚动已停止");
              console.log("note_result:", note_result); // 打印note_result
              //post传输数据
              //fetch()
              //excel导出数据
              var noteexcelname =$("title").text() +"笔记" + $(".filter span").text() + "采集数据.xlsx";
              downtoexcel(note_result, noteexcelname);
              var squareDiv = $('<div style="position: fixed;top: 200px;left:100;width: 400px;height: 400px;background: yellow;display: flex;align-items: center;justify-content: center;color: red;font-weight: bold;font-size: 24px;">评论成功</div>');
              // 将 div 添加到页面中
              $('body').append(squareDiv);
              return; // 退出函数
            }

            // 更新上一次滚动的位置
            lastScrollPosition = currentScrollPosition;
            console.log("本次实际滚动了", actualScrollAmount, "像素");
            checkForNewNoteItem();
          }, randomDelay);
        }
        window.addEventListener("scroll", checkForNewNoteItem);
        startScrollAndCheck();
      });

      //评论区采集按钮
      const scrollButton = $("<div>")
        .attr("id", "my-scroll")
        .css({
          width: "120px",
          height: "40px",
          background: "#fe2c55",
          position: "fixed",
          top: "130px",
          left: "30px",
          "z-index": "100000",
          "text-align": "center",
          "line-height": "40px",
          color: "#fff",
          "border-radius": "8px",
          cursor: "pointer",
        })
        .text("笔记评论采集");
      $("body").append(scrollButton);

      // 滚动按钮点击事件
      scrollButton.click(function () {
        console.log("开始滚动评论区", new Date());
        const commentsContainer = $(".note-scroller");
        let intervalId;
        function checkForEndContainer() {
          const endContainer = $(".comments-container").find(".end-container");
          if (endContainer.length > 0) {
            clearInterval(intervalId);
            getplinfo();
            console.log("已滚动到底部", new Date());
          }
        }

        function startScrollAndCheck() {
          let scrollTop = 0;
          const randomDelay =
            Math.floor(Math.random() * (1000 - 100 + 1)) + 100;
          intervalId = setInterval(() => {
            scrollTop += 100;
            commentsContainer.scrollTop(scrollTop);
            checkForEndContainer();
          }, randomDelay);
        }

        startScrollAndCheck();
      });

      function getplinfo() {
        const commentItems = $(".comment-item");
        const commentSubItems = $(".comment-item-sub");
        const allItems = commentItems.add(commentSubItems);
        const result = [];

        allItems.each(function () {
          const time = $(".note-content")
            .find(".bottom-container>.date")
            .text();
          const title = $(".note-content").find(".title").text();
          const avatarA = $(this).find(".avatar a");
          const authorA = $(this).find(".author a");
          const contentSpan = $(this).find(".note-text span");
          const likenum = $(this).find(".like .count");
          const pltime = $(this).find(".date span:first");
          const location = $(this).find(".location");
          let likes;
          if (likenum.text() === "赞") {
            likes = "0";
          } else {
            likes = likenum.text();
          }
          if (avatarA.length && authorA.length && contentSpan.length) {

            result.push({
              "笔记链接": window.location.href,
              "note_time": time,
              //title: title,
              "UID": avatarA.attr("data-user-id"),
              "昵称": authorA.text(),
              "评论内容": contentSpan.text(),
              "comment_time": pltime.text(),
              "地区": location.text(),
              "点赞": likes,
            });
          }
        });
        console.log(result);
       fetch('http://39.98.125.167:6324/cangk/to_mysql_comment/',{
           method: 'POST',
           headers: {
               'Content-Type': 'application/json',
           },
           body: JSON.stringify(result),
        })
        .then(response => response.json())
        .then(data => {
           var squareDiv = $('<div style="position: fixed;top: 200px;width: 400px;height: 400px;background: yellow;display: flex;align-items: center;justify-content: center;color: red;font-weight: bold;font-size: 24px;">评论成功</div>');
           //将 div 添加到页面中
           $('body').append(squareDiv);
           console.log('成功')
        })
        .catch((error) => {
           alert('失败，请查看控制台报错')
           console.error('错误', error);
        })

        //var excelname = window.location.href.split("?")[0] + "评论数据.xlsx";
        //downtoexcel(result, excelname);
      }
      //导出excel
      function downtoexcel(res, name) {
        const workbook = XLSX.utils.book_new();
        const worksheet = XLSX.utils.json_to_sheet(res);
        XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
        const excelData = XLSX.write(workbook, {
          bookType: "xlsx",
          type: "array",
        });
        const blob = new Blob([excelData], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        });
        const url = URL.createObjectURL(blob);
        const a = $("<a>");
        a.attr("href", url);
        a.attr("download", name);
        $("body").append(a);
        a[0].click();
        $("body").remove(a);
        URL.revokeObjectURL(url);
      }
    }
  );
});
